package com.anivia.system.auth.controller.admin;

import com.anivia.common.core.model.dto.BasePageDTO;
import com.anivia.common.core.model.vo.R;
import com.anivia.common.web.config.dataScope.DataScopeContext;
import com.anivia.common.web.satoken.admin.SaAdminCheckLogin;
import com.anivia.system.auth.enums.ResourceBaseEnum;
import com.anivia.system.auth.model.dto.RoleAddOrUpdateDTO;
import com.anivia.system.auth.service.IRoleInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author wangj
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("/admin/role")
public class RoleInfoAdminController {
    @Resource
    private IRoleInfoService roleInfoService;

    @Operation(summary = "新增或更新角色")
    @PostMapping("saveOrUpdate")
    @SaAdminCheckLogin
    private R saveOrUpdate(@RequestBody @Valid RoleAddOrUpdateDTO dto) {
        roleInfoService.saveOrUpdate(dto);
        return R.ok();
    }

    @Operation(summary = "查询角色分页")
    @GetMapping("page")
    @SaAdminCheckLogin
    private R page(@ParameterObject @Valid BasePageDTO dto) {
        return R.ok(roleInfoService.page(dto));
    }

    @Operation(summary = "查询角色详情")
    @GetMapping("{id}")
    @SaAdminCheckLogin
    private R details(@PathVariable Integer id) {
        return R.ok(roleInfoService.details(id));
    }

    @Operation(summary = "测试")
    @GetMapping("/test")
//    @SaAdminCheckLogin
    private R test() {
        // 开启数据权限
        DataScopeContext.start(ResourceBaseEnum.RESOURCE_2.getId());
        return R.ok(roleInfoService.list());
    }

    @Operation(summary = "删除角色")
    @PostMapping("delete/{id}")
    @SaAdminCheckLogin
    private R delete(@PathVariable Integer id) {
        roleInfoService.delete(id);
        return R.ok();
    }
}
